<template>
	<view class="content">
		<view class="row b-b heightauto">
			<text class="tit">提货点图片</text>
			<view class="select_imgs_public ">
				<view class="select_img_public select_img_public2" v-if="addressData.thumb.length>0">
					<image :src="addressData.thumb" @click="previewImg(0,addressData.thumb)"></image>
					<text @click="deleteImg(index,1)">×</text>
				</view>
				<view class="select_img_public select_img_public2" v-else @click="uplaodThumb(1)">
					<image class="reaname_thumb_upload" src="/static/shizi.jpg" mode=""></image>
				</view>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit">提货点名称</text>
			<input class="input" type="text" v-model="addressData.title" placeholder="提货点名称" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.pname" placeholder="联系人" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">联系电话</text>
			<input class="input" type="number" v-model="addressData.mobile" placeholder="联系电话" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">地图选址</text>
			<button type="default" size="mini" @click="openMap" style="margin:0;">定位</button>
		</view>
		<view class="row b-b">
			<text class="tit">提货点地址</text>
			<input class="input" type="text" v-model="addressData.address" placeholder="提货点地址" placeholder-class="placeholder" />
		</view>
		<view class="row b-b" @click="selectTime">
			<text class="tit">营业时间</text>
			<input class="input" type="text" disabled="true" v-model="addressData.saletime" placeholder="提货点营业时间" placeholder-class="placeholder" />
		</view>
		<smh-time-range v-show="isTime" :time="['08','00','0','22','00']" @confrim="confirmTime" @cancel="cancel"></smh-time-range>
		<view class="row b-b">
			<text class="tit">提货点人ID</text>
			<input class="input" type="number" v-model="addressData.pickupmid" placeholder="提货点人ID" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">备注</text>
			<input class="input" type="text" v-model="addressData.memo" placeholder="备注" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">是否开启</text>
			<switch :checked="addressData.status==1" color="#9532E4" @change="switch1Change" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
		<view class="fill" v-show="isShow">
			<view class="fill_item">
				<bt-cropper ref="cropper" :ratio="1/1" :imageSrc="imageSrc"></bt-cropper>
				<button @click="crop">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressData: {
					thumb: '',
					title: '',
					pname: '',
					mobile: '',
					address: '',
					saletime: '',
					pickupmid: '',
					x: "",
					x: "",
					memo: '',
					status: 1,
					id: 0,
				},
				id: 0,
				isShow: false,
				imageSrc: '',
				isTime: false,
			}
		},
		onLoad(option) {
			var that = this;
			let title = '新增自提点';
			that.id = option.id;
			that.addressData.id = option.id;
			if (option.id) {
				title = '编辑自提点'
				this.getInfo();
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			// 取消时间选择
			cancel(e){
				this.isTime = false;
			},
			confirmTime(e){
				this.addressData.saletime = e.time;
				this.isTime = false;
			},
			selectTime(){
				this.isTime = true;
			},
			switch1Change: function (e) {
				if(e.detail.value){
					this.addressData.status = 1;
				}else{
					this.addressData.status = 0;
				}
			},
			//提交
			confirm() {
				var that = this;
				let data = this.addressData;
				if(!data.thumb){
					return this.$api.msg('请上传提货点图片');
				}
				if (!data.title) {
					return this.$api.msg('请填写自提点名称');
				}
				if (!data.pname) {
					return this.$api.msg('请填写联系人');
				}
				if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(data.mobile)) {
					return this.$api.msg('请输入正确的手机号码');
				}
				if (!data.address) {
					return this.$api.msg('请填写提货点地址');
				}
				if (!data.saletime) {
					return this.$api.msg('请选择营业时间');
				}
				if(!data.pickupmid){
					return this.$api.msg('请填写提货点人ID');
				}
				this.$axios('market/pickupSave', "POST", 'supplier', data).then(res => {
					if (res.data.code == 200) {
						this.$api.msg('提交成功');
						setTimeout(()=>{
							uni.navigateTo({
								url: './supermarketZtd'
							})
						},1000)
					}
				})
			},
			getInfo(){
				this.$axios('market/pickupInfo', "POST", 'supplier', {
					id: this.id
				}).then(res => {
					if (res.data.code == 200) {
						this.addressData = res.data.data.info;
					}
				})
			},
			openMap() {
				let that = this;
				uni.chooseLocation({
					success(res) {
						that.addressData.address = res.address;
						that.addressData.y = res.latitude;
						that.addressData.x = res.longitude;
					}
				});
			},
			// 预览图片
			previewImg(idx,arr){
				if(typeof arr === 'string'){
					let arrs = [];
					arrs.push(arr);
					arr = arrs;
				}
				uni.previewImage({
					current: idx,
					urls: arr
				})
			},
			// 删除图片
			deleteImg(type) {
				this.addressData.thumb = {};
			},
			// 选择图片
			uplaodThumb(count) {
				uni.chooseImage({
					count: count,
					success:res=>{
						var path = res.tempFilePaths;
						path.forEach((image)=>{
							this.imageSrc = image;
							this.isShow = true;
						})
					}
				})
			},
			// 裁剪确认
			crop(){
			    // 通过组件定义的ref调用cropper方法，返回一个promise对象
			    this.$refs.cropper.crop().then((res)=>{
					this.realUpload(res);
			    })
			},
			// 上传服务器
			realUpload(image){
				var that = this;
				let arr = '';
				let token = uni.getStorageSync('token');
				uni.showLoading({
					title: '上传中,请稍后'
				});
				uni.uploadFile({
					url: that.$url.state.url.upload,
					filePath: image,
					name: 'a',
					header: {
						token: token
					},
					formData: {
						'file': 'a'
					},
					success: upres => {
						let data = JSON.parse(upres.data);
						this.isShow = false;
						if (data.code == 200) {
							arr = data.data.url;
							that.addressData.thumb = arr;
							uni.hideLoading();
						}
					},
					fail() {
						setTimeout(() => {
							that.$api.msg('上传失败，请重试')
						}, 100)
					},
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}
	.fill{
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(#000,.8);
		z-index: 999999;
		display: flex;
		align-items: center;
		justify-content: center;
		.fill_item{
			width: 800rpx;
			height: 500rpx;
		}
	}
	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			margin-right: 10upx;
			width: 170rpx;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.heightauto{
		height: auto;
		padding: 20rpx 30rpx;
	}
	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(113, 57, 69, 0.4);
	}
</style>